// 日期
export function formatDate (originVal) {
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')

  return `${y}-${m}-${d}`
}

// 时间戳转换为时间格式字符串
export function formatTime (date, format) {
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(format)) {
      const str = o[k] + ''
      format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return format
}

function padLeftZero (str) {
  return ('00' + str).substr(str.length)
}

// 时长封装
export function timeFormat (time) {
  // console.log(time)
  const timee = parseInt(time / 1000)
  // console.log(timee)
  // 分钟
  var minute = timee / 60
  var minutes = parseInt(minute)

  if (minutes < 10) {
    minutes = '0' + minutes
  }

  // 秒
  var second = timee % 60
  var seconds = Math.round(second)
  if (seconds < 10) {
    seconds = '0' + seconds
  }
  return `${minutes}:${seconds}`
}
